package com.jinyu.jyly.ui.mine.invite;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.jinyu.jyly.R;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.bean.ConfigInfo;
import com.jinyu.jyly.utils.AES;
import com.yzq.zxinglibrary.encode.CodeCreator;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 邀请
 */
public class InviteActivity extends BaseActivity<InvitePresenter> implements InviteContract.View {

    @BindView(R.id.fake_status_bar)
    View fakeStatusBar;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_invite_code)
    TextView tvInviteCode;
    @BindView(R.id.iv_code)
    ImageView ivCode;
    @BindView(R.id.rl_title_view)
    RelativeLayout rlTitleView;

    private InvitePresenter presenter;

    @Override
    public int getLayout() {
        return R.layout.activity_invite;
    }

    @Override
    protected InvitePresenter getPresenter() {
        if(presenter == null){
            presenter = new InvitePresenter(this, this);
        }
        return presenter;
    }

    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarDarkMode(this);
        presenter.getConfig();
    }



    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }


    @Override
    public void onFailer(Throwable throwable) {

    }

    /**
     * 获取配置
     * @param infoBean
     */
    @Override
    public void onGetConfig(AllInfoBean infoBean) {
        switch (infoBean.getStatus()) {
            case 200:
                String data = infoBean.getData();
                try {
                    String info = AES.decrypt(data);
                    Gson gson = new Gson();
                    ConfigInfo configInfo = gson.fromJson(info, ConfigInfo.class);
                    if (configInfo != null) {
                        tvInviteCode.setText("推荐码："+configInfo.getPromotion());
                        Bitmap bitmap = CodeCreator.createQRCode(configInfo.getShare_link(), 260, 260, null);
                        ivCode.setImageBitmap(bitmap);

//                        SharedPreferencesUtils.setParam(MainActivity.this, GlobalConstants.LOGIN_PROMOTION, configInfo.getPromotion());
//                        SharedPreferencesUtils.setParam(MainActivity.this, GlobalConstants.SHARE_LINK, configInfo.getShare_link());
//                        SharedPreferencesUtils.setParam(MainActivity.this, GlobalConstants.IN_NUM, configInfo.getIn_num());
//                        SharedPreferencesUtils.setParam(MainActivity.this, GlobalConstants.CONFIG_RECHARGE_RATE, configInfo.getRecharge_rate());
//                        SharedPreferencesUtils.setParam(MainActivity.this, GlobalConstants.CONFIG_RECHARGE_MIN, configInfo.getRecharge_min());

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:
                break;
        }
    }
}
